<?php
declare (strict_types=1);

namespace app\controller;

use app\validate\Upload;
use app\model\FileUploadRecord;
use app\traits\CommonTrait;
use app\utils\QcloudCosUpload;
use think\facade\Request;
use think\facade\Session;
use think\facade\Filesystem;
use think\exception\ValidateException;

class FileUpload
{

    use CommonTrait;

    public function create()
    {
        $host = Request::server('HTTP_HOST');
        $scheme = Request::server('REQUEST_SCHEME','https');
        $uid = Session::get('uid', 0);
        //jpg png bmp gif
        $img = Request::file('image');
        try {
            validate(Upload::class)->check(['image' => $img]);
        } catch (\think\exception\ValidateException $e) {
            return $this->apiJson(-1, $e->getError());
        }
        $ext = strtolower($img->getOriginalExtension());
        $hashName = $img->hash('md5');
        $fileUploadInfo = FileUploadRecord::where(['hash_name' => $hashName])->field('file_path,file_type')->findOrEmpty();
        if (!$fileUploadInfo->isEmpty()) {
            $url = $scheme . '://' . $host . '/storage/' . $fileUploadInfo['file_path'] . $hashName . '.' . $fileUploadInfo['file_type'];
            return $this->apiJson(1, 'success', ['url' => $url]);
        }
        try {
            $fileName = Filesystem::disk('public')->putFileAs('uploads', $img, $hashName . '.' . $ext);
        } catch (ValidateException $e) {
            return $this->apiJson(-1, $e->getMessage());
        }
        $i = strrpos($fileName, '/');
        $filePath = substr($fileName, 0, $i) . '/';
        $url = $scheme . '://' . $host . '/storage/' . $fileName;
        FileUploadRecord::create(['user_id' => $uid, 'hash_name' => $hashName, 'file_path' => $filePath, 'file_type' => $ext]);
        return $this->apiJson(1, 'success', ['url' => $url]);
    }

    //腾讯云上传
    public function ossCreate()
    {
        $uid = Session::get('uid', 0);
        $baseUrl = 'http://upload.douwuku.com';
        //jpg png bmp gif
        $img = Request::file('image');
        try {
            validate(Upload::class)->check(['image' => $img]);
        } catch (\think\exception\ValidateException $e) {
            return $this->apiJson(-1, $e->getError());
        }
        $realPath = $img->getRealPath();
        $ext = strtolower($img->getOriginalExtension());
        $hashName = $img->hash('md5');
        $fileUploadInfo = FileUploadRecord::where(['hash_name' => $hashName])->field('file_path,file_type')->findOrEmpty();
        if (!$fileUploadInfo->isEmpty()) {
            $url = $baseUrl . '/publicdata/' . $fileUploadInfo['file_path'] . $hashName . '.' . $fileUploadInfo['file_type'];
            return $this->apiJson(1, '上传图片成功', ['url' => $url]);
        }
        $fileName = '/publicdata/images/' . $hashName . '.' . $ext;
        try {
            $cos = new QcloudCosUpload();
            $cos->upload(1, $fileName, $realPath);
        } catch (\Exception $exception) {
            return $this->apiJson(-1, '上传图片失败');
        }
        $url = $baseUrl . $fileName;
        FileUploadRecord::create(['user_id' => $uid, 'hash_name' => $hashName, 'file_path' => 'images/', 'file_type' => $ext]);
        return $this->apiJson(1, '上传图片成功', ['url' => $url]);
    }

}
